package swd.work.generator.v1.java.bean;

import java.io.*;
import java.util.*;

import com.pub.database.*;
import com.pub.tools.*;

import swd.work.generator.v1.java.base.*;

public class PropertiesBean extends BeanExtend {
    String gstrZH_TWFileName = "Multi-lang_zh_TW.properties";
    String gstrZH_TWSrcFileName = "Multi-lang_src_zh_TW.properties";
    String gstrDefaultFileName = "Multi-lang.properties";
    BufferedWriter gbwrZH_TWFile = null;
    BufferedWriter gbwrZH_TWSrcFile = null;
    BufferedWriter gbwrDefaultFile = null;


    public PropertiesBean(QueryBean pqbnDAO, String pstrUserNO) throws
            Exception {
        super(pqbnDAO, pstrUserNO);
    }

    private void outputProperties(String pstrKeyName, String pstrKeyValueC,
                                  String pstrKeyValueE) throws Exception {
        if (pstrKeyValueC == null) {
            pstrKeyValueC = "null";
        }
        if (pstrKeyValueE == null) {
            pstrKeyValueE = "null";
        }

        super.outputJavaCode(gbwrZH_TWSrcFile, null, null,
                             pstrKeyName + "=" + pstrKeyValueC, 1);
        super.outputJavaCode(gbwrZH_TWFile, null, null,
                             pstrKeyName + "=" +
                             PublicFunction.
                             getDoubleBytesStrUnicode(pstrKeyValueC), 1);
        super.outputJavaCode(gbwrDefaultFile, null, null,
                             pstrKeyName + "=" + pstrKeyValueE, 1);
    }

    /**
     * setBtnTitle
     *
     * @param pbwrZH_TWFile BufferedWriter
     * @param pbwrDefaultFile BufferedWriter
     */
    private void setBtnTitle() throws Exception {
        outputProperties("BtnAddTitle", "新增", "AddNew");
        outputProperties("BtnSaveTitle", "存檔", "Save");
        outputProperties("BtnQueryTitle", "查詢", "Query");
        outputProperties("BtnModifyTitle", "修改", "Modify");
        outputProperties("BtnApproveTitle", "簽核", "Approve");
        outputProperties("BtnRejectTitle", "駁回", "Rejct");
        outputProperties("BtnDraftTitle", "草稿", "Draft");
        outputProperties("BtnCancelTitle", "取消", "Cancel");
        outputProperties("BtnUpdateTitle", "更新", "Update");
        outputProperties("BtnExitTitle", "離開", "Exit");
        outputProperties("BtnUploadTitle", "上傳檔案", "Update");
        outputProperties("BtnNoteTitle", "審核意見", "Approval Comment");
        outputProperties("BtnTraceTitle", "跟催", "Flow Trace");
    }

    /**
     * setFrmName
     *
     * @param pstrPrgNO String
     */
    private void setFrmName(String pstrPrgNO) throws Exception {
        Hashtable<String,Hashtable<String,Object>> lhtFrm0 = null;

        try {
            lhtFrm0 = gqbnDAO.hashtableSQL(
                    "select frm_no,frm_name from wkf_frm0 where prg_no='" +
                    pstrPrgNO + "'");

            if (lhtFrm0 == null || lhtFrm0.get("0") == null) {
                throw new Exception("no " + pstrPrgNO + "'s form name");
            }

            for (int i = 0; i < lhtFrm0.size(); i++) {
                outputProperties("Frm" +
                                 ((Hashtable<String,Object>) lhtFrm0.get(String.valueOf(i))).
                                 get("FRM_NO").toString() + "Name",
                                 ((Hashtable<String,Object>) lhtFrm0.get(String.valueOf(i))).
                                 get("FRM_NAME").toString(),
                                 ((Hashtable<String,Object>) lhtFrm0.get(String.valueOf(i))).
                                 get("FRM_NAME").toString());
            }
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            if (lhtFrm0 != null) {
                lhtFrm0.clear();
                lhtFrm0 = null;
            }
        }
    }

    /**
     * setPrgTitle
     *
     * @param pstrPrgNO String
     * @param pbwrZH_TWFile BufferedWriter
     * @param pbwrDefaultFile BufferedWriter
     */
    private void setPrgTitle(String pstrPrgNO) throws Exception {
        Hashtable<String,Object> lhtWKF_PRG = null;

        try {
            lhtWKF_PRG = gqbnDAO.readSingleRowData(
                    "select prg_name,prg_name_e from wkf_prg where prg_no='" +
                    pstrPrgNO + "'");

            outputProperties("prgTitle", lhtWKF_PRG.get("PRG_NAME").toString(),
                             lhtWKF_PRG.get("PRG_NAME_E").toString());
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    public String createProperties(String pstrPrgNO) {
        String lstrCreateResult = "";
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM1 = null;
        int lintWKF_FRM1_cnt = 0;
        Hashtable<String,Object> lhtWKF_FRM1_DATA = null;

        try {
            gbwrZH_TWFile = getFwrPrgNO(gstrZH_TWFileName, "jsp_frame_path",
                                        "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                        "\\" + pstrPrgNO + "\\", gstrUserNO);
            gbwrZH_TWSrcFile = getFwrPrgNO(gstrZH_TWSrcFileName,
                                           "jsp_frame_path",
                                           "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                           "\\" + pstrPrgNO + "\\", gstrUserNO);
            gbwrDefaultFile = getFwrPrgNO(gstrDefaultFileName, "jsp_frame_path",
                                          "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                          "\\" + pstrPrgNO + "\\", gstrUserNO);

            super.setBaseVarible(pstrPrgNO);

            setPrgTitle(pstrPrgNO);
            setFrmName(pstrPrgNO);
            setBtnTitle();

            lhtWKF_FRM1 = gqbnDAO.hashtableSQL(
                    "select fld_no,fld_title_c,fld_title_e from wkf_frm1 where prg_no='" +
                    pstrPrgNO +
                    "' and fld_no is not null group by fld_no,fld_title_c,fld_title_e");

            while (lhtWKF_FRM1 != null &&
                   lhtWKF_FRM1.get(String.valueOf(lintWKF_FRM1_cnt)) != null) {

                lhtWKF_FRM1_DATA = (Hashtable<String,Object>) lhtWKF_FRM1.get(String.valueOf(
                        lintWKF_FRM1_cnt));

                outputProperties(lhtWKF_FRM1_DATA.get("FLD_NO").toString() +
                                 "Title",
                                 lhtWKF_FRM1_DATA.get("FLD_TITLE_C").toString(),
                                 lhtWKF_FRM1_DATA.get("FLD_TITLE_E").toString());

                lintWKF_FRM1_cnt++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                gbwrZH_TWFile.flush();
                gbwrZH_TWFile.close();
                gbwrZH_TWFile = null;
            } catch (Exception e) {
                System.out.println("cant flush ZH_TW properties File!!");
                e.printStackTrace();
            }
            try {
                gbwrZH_TWSrcFile.flush();
                gbwrZH_TWSrcFile.close();
                gbwrZH_TWSrcFile = null;
            } catch (Exception e) {
                System.out.println("cant flush ZH_TW src properties File!!");
                e.printStackTrace();
            }
            try {
                gbwrDefaultFile.flush();
                gbwrDefaultFile.close();
                gbwrDefaultFile = null;
            } catch (Exception e) {
                System.out.println("cant flush Default properties File!!");
                e.printStackTrace();
            }
            if (lhtWKF_FRM1 != null) {
                lhtWKF_FRM1.clear();
                lhtWKF_FRM1 = null;
            }
            if (lhtWKF_FRM1_DATA != null) {
                lhtWKF_FRM1_DATA.clear();
                lhtWKF_FRM1_DATA = null;
            }  
        }
        
        return lstrCreateResult;
    }


}
